import { PrfData } from "../classes/PrfData";
import { readUriToTextAndExplode } from "../readFileToText/openTextFile";
import { RenderData } from "../classes/RadarRenderData";
import { loadSymbologyFile } from "./loadSymbologyFile";
import { path } from "@tauri-apps/api";
import { loadIcaoAircraftFile } from "./loadIcaoAircraftFile";
import { loadIcaoAirlinesFile } from "./loadIcaoAirlinesFile";
import { loadIcaoAirportsFile } from "./loadIcaoAirportFile";
import { loadServerFile } from "./loadServerFile";
import { loadAsrFile } from "./loadAsrFile";
import { loadSctFile } from "./loadSctFile";
import { loadEseFile } from "./loadEseFile";
import { RadarScreen } from "../RadarCanvasDrawer";

export async function loadPrfFile(uri: string) {
    const result: PrfData = new PrfData();
    const directory = await path.dirname(uri);
    const reg = new RegExp('(?<type>[^\\t]*)\\t(?<name>[^\\t]*)\\t(?<value>[^\\t]*)');
    const lines = await readUriToTextAndExplode(uri, ';');
    console.log('prf liens', lines);
    for (const line of lines) {
        if (reg.test(line)) {
            const match = reg.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                if (g.type == "Settings") {
                    if (g.name == "SettingsfileSYMBOLOGY") {
                        const targetPath = await path.join(directory, g.value);
                        await loadSymbologyFile(targetPath);
                    } else if (g.name == "aircraft") {
                        const targetPath = await path.join(directory, g.value);
                        await loadIcaoAircraftFile(targetPath);
                    } else if (g.name == "airlines") {
                        const targetPath = await path.join(directory, g.value);
                        await loadIcaoAirlinesFile(targetPath);
                    } else if (g.name == "airports") {
                        const targetPath = await path.join(directory, g.value);
                        await loadIcaoAirportsFile(targetPath);
                    } else if (g.name == "myipaddr") {
                        const targetPath = await path.join(directory, g.value);
                        await loadServerFile(targetPath);
                    } else if (g.name == "sector") {
                        const targetPath = await path.join(directory, g.value);
                        await loadSctFile(targetPath);
                        const esePath = await path.join(directory, g.value.replace('.sct', '.ese'));
                        await loadEseFile(esePath);
                    }
                } else if (g.type == "RecentFiles") {
                    const targetPath = await path.join(directory, g.value);
                    await loadAsrFile(targetPath);
                }
                result.settings.push({
                    type: g.type,
                    name: g.name,
                    value: g.value
                });
            }
        }
    }
    console.log('prf read', result);
    RenderData.prf.set(uri, result);
    RadarScreen.resetInitDraw();
}